#include "Mailbox.h"

Mailbox::Mailbox()
{
}

Mailbox::~Mailbox()
{
}

void Mailbox::link(DataStore* input)
{
    ds = input;
}

void Mailbox::showMail(string username)
{
    cout << "Your mails are displayed below." << endl
            << "New mails since your last visit have asterisks (*) around them."
            << endl << endl;
    int size = ds->mails.size();
    for (int i = 0; i < size; i++)
    {
        if (username == ds->mails[i].user)
        {
            if (ds->mails[i].read == false)
                cout << "***";
            cout << " " << ds->mails[i].contents << " ";
            if (ds->mails[i].read == false)
                cout << "***";
            cout << endl;
            ds->mails[i].read = true;
        }
    }
    cout << endl;
}

void Mailbox::sendMail(string contents)
{
    deque<string> clients = getClientList();
    int size = clients.size();
    for (int i = 0; i < size; i++)
    {
        mail temp;
        temp.read = false;
        temp.user = clients[i];
        temp.contents = contents;
        ds->mails.push_back(temp);
    }
}

deque<string> Mailbox::getClientList()
{
    deque<string> temp;
    int size = ds->accounts.size();
    for (int i = 0; i < size; i++)
    {
        if (ds->accounts[i].type == Client)
            temp.push_back(ds->accounts[i].user);
    }
    return temp;
}